package com.vb.controller;

import com.vb.entity.CableShop;
import com.vb.service.CableShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@RestController
@AllArgsConstructor
@RequestMapping("/shop")
@Api(value = "商品列表", tags = "商品列表")
public class CableShopController {

    private final CableShopService cableShopService;

    /**
     * 添加商品
     *
     * @param cableShop
     * @return
     */
    @PostMapping("/save")
    @ApiOperation(value = "添加商品", notes = "添加商品")
    public Map<String, String> saveCableShop(CableShop cableShop) {
        Map<String, String> map = new HashMap<>();
        boolean save = cableShopService.save(cableShop);
        if (save) {
            map.put("msg", "商品添加成功！");
            return map;
        } else {
            map.put("msg", "商品添加失败！");
            return map;
        }
    }

    /**
     * 删除商品
     */
    @DeleteMapping("/delShop")
    @ApiOperation(value = "删除商品", notes = "删除商品")
    public Map<String, String> delShop(Long[] ids) {
        Map<String, String> map = new HashMap<>();
        boolean b = cableShopService.removeBatchByIds(Stream.of(ids).collect(Collectors.toList()));
        if (b) {
            map.put("msg", "商品删除成功！");
            return map;
        } else {
            map.put("msg", "商品删除失败！");
            return map;
        }
    }

    /**
     * 修改商品
     * @param cableShop
     * @return
     */
    @PutMapping("/updShop")
    @ApiOperation(value = "修改商品",notes = "修改商品")
    public Map<String,String> updShop(CableShop cableShop){
        Map<String, String> map = new HashMap<>();
        boolean b = cableShopService.updateById(cableShop);
        if (b) {
            map.put("msg", "商品修改成功！");
            return map;
        } else {
            map.put("msg", "商品修改失败！");
            return map;
        }
    }

    /**
     * 所有商品列表
     */
    @GetMapping("/page")
    @ApiOperation(value = "所有商品列表",notes = "所有商品列表")
    public Map<String,Object> list(){
        Map<String,Object> map = new HashMap<>();
        List<CableShop> list = cableShopService.list();
        if (map != null){
            map.put("msg","列表查询成功1");
            map.put("data",list);
            return map;
        }else {
            map.put("msg","列表查询失败！");
            map.put("data",null);
            return map;
        }
    }

}
